﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;
using HA.Data;
namespace HA.ModFlow.Controls.Windows.InternalFlow
{
    /// <summary>
    /// winBCFFlow.xaml 的交互逻辑
    /// </summary>
    public partial class winBCFFlow : Window
    {

        public winBCFFlow(Project proj)
        {
            InitializeComponent();
            project = proj;
            ModFlowData mfData = proj.MfData;

            this._pCBCFlow = new PCBCFFlow(mfData.FlowData.BcfData);

            //_bcfInner = new PCBCFInner(inner);
            mIsHave = this._pCBCFlow.BCFData.Parent.IsHaveTransient;
            this._mdOption = mfData.ModelOption;
            ComboBoxItem cbo;
            for (int i = 0; i < this._pCBCFlow.CloneCopy.Layers.Count; i++)
            {
                cbo = new ComboBoxItem();
                cbo.Content = (i + 1).ToString();
                this.cboLayer.Items.Add(cbo);
            }
            this.cboLayer.SelectedIndex = 0;
           
        }
        Project project;

        ModelOption _mdOption;
        private bool mIsHave = false;  
        PCBCFFlow _pCBCFlow;
        public PCBCFFlow PCBCFlow
        {
            get { return _pCBCFlow; }
            set { _pCBCFlow = value; }
        }

        

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._pCBCFlow.Confirm();
            this.DialogResult = true;
            this.Close();
        }

       

        /// <summary>
        /// 更新层类型数据状态
        /// </summary>
        /// <param name="Index"></param>
        private void UpdataLayerType(int Index)
        {
            if (this._pCBCFlow.CloneCopy.Layers[Index].LayerType == (BCFLAYERTYPE)LAYERTYPE.CONFINED)
            {
                this.rbtn1.IsChecked  = true;               
            }
            else if (this._pCBCFlow.CloneCopy.Layers[Index].LayerType == (BCFLAYERTYPE)LAYERTYPE.NOCONFINED)
            {
                this.rbtn2.IsChecked = true;
            }
            else if (this._pCBCFlow.CloneCopy.Layers[Index].LayerType == (BCFLAYERTYPE)LAYERTYPE.TRANSMISSITYCONSTANT)
            {
                this.rbtn3.IsChecked = true;
            }
            else
            {
                this.rbtn4.IsChecked = true;
            }
        }
        /// <summary>
        /// 设置层类型状态
        /// </summary>
        /// <param name="Index"></param>
        private void SetLayerType(int Index)
        {
            if (this.rbtn1.IsChecked.Value)
            {
                this._pCBCFlow.CloneCopy.Layers[Index].LayerType = (BCFLAYERTYPE)LAYERTYPE.CONFINED;
            }
            else if (this.rbtn2.IsChecked.Value)
            {
                this._pCBCFlow.CloneCopy.Layers[Index].LayerType = (BCFLAYERTYPE)LAYERTYPE.NOCONFINED;
            }
            else if (rbtn3.IsChecked.Value)
            {
                this._pCBCFlow.CloneCopy.Layers[Index].LayerType = (BCFLAYERTYPE)LAYERTYPE.TRANSMISSITYCONSTANT;
            }
            else
            {
                this._pCBCFlow.CloneCopy.Layers[Index].LayerType = (BCFLAYERTYPE)LAYERTYPE.TRANSMISSITYVARY;
            }
        }

        private void cboLayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboLayer.SelectedIndex == 0)
            {
                this.rbtn2.IsEnabled = true ;
            }
            else
            {
                this.rbtn2.IsEnabled = false;
                this.rbtn2.IsChecked = false;
            }
            if (this.cboLayer.SelectedIndex == this.cboLayer.Items.Count - 1)
            {
                this.btnLeakance.IsEnabled = false;
            }
            else
            {
                this.btnLeakance.IsEnabled = true;
            }
            if (this.cboLayer.SelectedIndex != -1)
            {
                Binding binding = new Binding();
                binding.Source = _pCBCFlow.CloneCopy.Layers[this.cboLayer.SelectedIndex];
                binding.Path = new PropertyPath("TRPY");
                this.txtTRPY.SetBinding(TextBox.TextProperty, binding);
                this.cboLtype.SelectedIndex = _pCBCFlow.CloneCopy.Layers[this.cboLayer.SelectedIndex].LAYAVG;                
            }
            UpdataLayerType(this.cboLayer.SelectedIndex);
            if (this.rbtn2.IsEnabled == false && this.rbtn2.IsChecked.Value)
            {
                this.rbtn1.IsChecked = true;
            }

            if (mIsHave)
            {
                this.btnSf1.IsEnabled = true;
            }
            else
            {
                this.btnSf1.IsEnabled = false ;
            }
            Uprbtn1();
            Uprbtn2();
            Uprbtn3();
            Uprbtn4();
        }

        private void btnTrans_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.Transmissivity,project);
            win.Owner = this;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "^2/" + Domain.DomainLayer.UnitConverter.GetTime(_mdOption.Units.Time) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnHydraulic_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.Parent.Hk, project);
            win.Owner = this;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(_mdOption.Units.Time) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnLeakance_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.Leakance, project);
            win.Owner = this;
            win.Unit += "(1/" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnSf1_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.PrimaryStorage, project);
            win.Owner = this;
            win.Unit += "无量纲";
            if (win.ShowDialog().Value)
            {
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.OnCurrentDSChanged();
            }

        }

        private void btnSf2_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.SecondaryStorage, project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnWetDryFlag_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._pCBCFlow.CloneCopy.Parent.WetDry, project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

      

        private void rbtn1_Click(object sender, RoutedEventArgs e)
        {
            Uprbtn1();          
        }

        private void Uprbtn1()
        {
            if (this.rbtn1.IsChecked.Value)
            {
                this.btnTrans.IsEnabled = true;
                this.btnHydraulic.IsEnabled = false;
                SetLayerType(this.cboLayer.SelectedIndex);
                if (mIsHave && this.rbtn1.IsChecked.Value)
                {
                    this.btnSf2.IsEnabled = false;
                }
                else if (mIsHave == false)
                {
                    this.btnSf2.IsEnabled = false;
                }
            }
        }

        private void rbtn2_Click(object sender, RoutedEventArgs e)
        {
            Uprbtn2();
        }
        private void Uprbtn2()
        {
            if (this.rbtn2.IsChecked.Value)
            {
                this.btnTrans.IsEnabled = false;
                this.btnHydraulic.IsEnabled = true;
                SetLayerType(this.cboLayer.SelectedIndex);

                if (mIsHave && this.rbtn2.IsChecked.Value)
                {
                    this.btnSf2.IsEnabled = false;
                }
                else if (mIsHave == false)
                {
                    this.btnSf2.IsEnabled = false;
                }
            }
        }

        private void rbtn3_Click(object sender, RoutedEventArgs e)
        {
            Uprbtn3();
        }

        private void Uprbtn3()
        {
            if (this.rbtn3.IsChecked.Value)
            {
                this.btnTrans.IsEnabled = true;
                this.btnHydraulic.IsEnabled = false;
                SetLayerType(this.cboLayer.SelectedIndex);
                if (rbtn3.IsChecked.Value && mIsHave)
                {
                    this.btnSf2.IsEnabled = true;
                }
            }
        }

        private void rbtn4_Click(object sender, RoutedEventArgs e)
        {
            Uprbtn4();
        }
        private void Uprbtn4()
        {
            if (this.rbtn4.IsChecked.Value)
            {
                this.btnTrans.IsEnabled = false;
                this.btnHydraulic.IsEnabled = true;
                SetLayerType(this.cboLayer.SelectedIndex);
                if (rbtn4.IsChecked.Value && mIsHave)
                {
                    this.btnSf2.IsEnabled = true;
                }
            }
        }

        private void cboLtype_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
        }

        private void cboLtype_DropDownClosed(object sender, EventArgs e)
        {
            this._pCBCFlow.CloneCopy.Layers[this.cboLayer.SelectedIndex].LAYAVG = this.cboLtype.SelectedIndex;
        }

        private void chkAllow_Checked(object sender, RoutedEventArgs e)
        {
            chkAllow_Click(sender, e);
        }

        private void chkAllow_Click(object sender, RoutedEventArgs e)
        {
            if (this.chkAllow.IsChecked.Value)
            {
                this.btnWetDryFlag.IsEnabled = true;
                this.txtWETFCT.IsEnabled = true;
                this.txtIWETIT.IsEnabled = true;
                this.cboIHDWET.IsEnabled = true;
            }
            else
            {
                this.btnWetDryFlag.IsEnabled = false;
                this.txtWETFCT.IsEnabled = false;
                this.txtIWETIT.IsEnabled = false;
                this.cboIHDWET.IsEnabled = false;
            }
        }

        private void rbtn1_Checked(object sender, RoutedEventArgs e)
        {
            rbtn1_Click(sender, e);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this._pCBCFlow;  
            chkAllow_Checked(sender, e);
            Uprbtn1();
            Uprbtn2();
            Uprbtn3();
            Uprbtn4();
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

     

      
     




      
    }
}